import Vue from 'vue'
import VueRouter from 'vue-router'

//方法
import { getToken } from '@/utils/storage'
import { Toast } from 'vant'

// 一级路由
import Login from '@/views/Login'
import Register from '@/views/Register'
import Home from '@/views/Home'
import Favorites from '@/views/Favorites'
import City from '@/views/City'
import House from '@/views/House'
import NoHouse from '@/views/NoHouse'
import AddHouse from '@/views/AddHouse'
import Map from '@/views/Map'

// 二级路由
import My from '@/views/Home/My'
import Find from '@/views/Home/Find'
import HomePage from '@/views/Home/HomePage'
import Consulting from '@/views/Home/Consulting'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/register',
    component: Register
  },
  {
    path: '/favorites',
    component: Favorites
  },
  {
    path: '/city',
    component: City
  },
  {
    path: '/house',
    component: House
  },
  {
    path: '/map',
    component: Map
  },
  {
    path: '/nohouse',
    component: NoHouse
  },
  {
    path: '/addhouse',
    component: AddHouse
  },
  {
    path: '/home',
    component: Home,
    children: [
      {
        path: '/home',
        redirect: '/home/homePage'
      },
      {
        path: '/home/consulting',
        component: Consulting
      },
      {
        path: '/home/find',
        component: Find
      },
      {
        path: '/home/homePage',
        component: HomePage
      },
      {
        path: '/home/my',
        component: My
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

export default router

// 全局前置守卫：

const BlankList = ['/favorites', '/house', '/addhouse']

// const arr = ["/login", "/register"]

router.beforeEach((to, from, next) => {
  if (!getToken() && BlankList.includes(to.path)) {
    Toast.fail('没有授权，请登录再试')
    next('/login')
  } else {
    next()
  }
})

// export default router;
